package v1

import (
	"bytes"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"io/ioutil"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/pkg/util"
	"zimuzu_cvn_web_api/router/api"
	"zimuzu_cvn_web_api/router/api/app/middleware"
	"zimuzu_cvn_web_api/service"
)

type YqbAPI struct {
}

func (yqb YqbAPI) Name() string {
	return "yqb API"
}

func (yqb YqbAPI) Bind() {
	YqbAPI := config.C.Router.Group("/v1/app/amount", middleware.TrackLog(), middleware.AuthMiddleware())
	{
		YqbAPI.GET("rechargeAddress", rechargeAddress)
		YqbAPI.GET("serviceCharge", serviceCharge)
		YqbAPI.GET("balance", balance)
		YqbAPI.POST("checkPayPassword", checkPayPassword)
		YqbAPI.POST("transfer", transfer)
		YqbAPI.GET("isCvnWallet", isCvnWallet)
	}
}

//获取充值地址
//Auth: TXG
func rechargeAddress(c *gin.Context) {
	data := make(map[string]string)
	uid, _ := service.GetUserIdByRequest(c)
	address, err := service.GetWalletAddress(uid)
	if err != nil {
		api.SendResponse(c, errno.GetDataErr, nil)
		return
	}
	data["wallet"] = address
	errno.OK.Info = "OK"
	api.SendResponse(c, errno.OK, data)
	return
}

//手续费
//Auth: TXG
func serviceCharge(c *gin.Context) {
	var data model.ServiceCharge
	data.Charge = service.GetCharges()
	errno.OK.Info = "OK"
	api.SendResponse(c, errno.OK, data)
	return
}

//账户可用余额
//Auth: TXG
func balance(c *gin.Context) {
	uid, _ := service.GetUserIdByRequest(c)
	data, err := service.GetAmountBalance(uid)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	errno.OK.Info = "OK"
	api.SendResponse(c, errno.OK, data)
	return
}

//校验支付密码
//Auth: TXG
func checkPayPassword(c *gin.Context) {
	var (
		data   model.Uuid
		params model.PayPasswordParam
	)
	data.NeedCode = 1
	uid, _ := service.GetUserIdByRequest(c)
	author := c.Request.Header.Get("Authorization")
	if err := c.ShouldBindWith(&params, binding.FormPost); err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	password := params.PayPassword
	u, err := service.CheckPayPassword(uid, password)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	if u == "" {
		api.SendResponse(c, errno.SystemErr, nil)
		return
	}
	codeChecked, err := service.IsTransferSmsCodeChecked(uid, author)
	if err != nil {
		api.SendResponse(c, errno.GetDataErr, nil)
		return
	}
	if codeChecked {
		data.NeedCode = 0
	}
	data.U = u
	errno.OK.Info = "OK"
	api.SendResponse(c, errno.OK, data)
	return
}

//转账提交
//Auth: TXG
func transfer(c *gin.Context) {
	var (
		getData      model.Sign
		postData     model.TransferParam
		responseData model.Id
	)
	uid, _ := service.GetUserIdByRequest(c)
	author := c.Request.Header.Get("Authorization")
	//获取get参数
	if err := c.ShouldBindWith(&getData, binding.Query); err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	sign := getData.Sign
	//获取body
	bodyBytes, err := ioutil.ReadAll(c.Request.Body)
	bodyStr := string(bodyBytes)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))

	if err := c.ShouldBindWith(&postData, binding.FormPost); err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	ok, err := util.AppCheckSign(bodyStr, author, sign)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	if !ok {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	id, err := service.Transfer(uid, author, postData)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	responseData.Id = id
	errno.OK.Info = "OK"
	api.SendResponse(c, errno.OK, responseData)
	return
}

//是否为站内钱包
//Auth: TXG
func isCvnWallet(c *gin.Context) {
	var (
		data   model.Is
		params model.WalletParam
	)
	if err := c.ShouldBindWith(&params, binding.Query); err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	_, ok, err := service.GetCheckAddress(params.Wallet)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	if ok {
		data.Is = 1
	} else {
		data.Is = 0
	}
	errno.OK.Info = "OK"
	api.SendResponse(c, errno.OK, data)
	return
}
